﻿using System;
using EntitiesTM;
using Business;

namespace TimeWork.time.admin
{
    public partial class EditClient : System.Web.UI.Page
    {
        private string getParam()
        {
            if (Request.QueryString["cid"] != null)
            {
                return Request.QueryString["cid"].ToString();
            }
            else
            {
                return "0";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["email"] == null)
            {
                Response.Redirect("http://"+Request.Url.Authority+"/Login.aspx");
            }
            if (!IsPostBack)
            {
                LoadContent();
            }
        }

        public void LoadContent()
        {
            string cid = getParam();
            if (!cid.Equals("0"))
            {
                EnClient client = BuClient.selClientByID(Int32.Parse(cid), Session["companyid"].ToString());
                txtClientName.Text = client.clientName;
                txtURL.Text = client.url;
                txtDescription.Text = client.clientDes;
                txtAddress.Text = client.clientAddress;
                txtNameAlt.Text = client.nameAlt;
                txtNameContact.Text = client.contactPerson;
                txtEmail.Text = client.email;
                txtPhone.Text = client.phone;
                txtFax.Text = client.fax;
            }
            else
            {
                Response.Redirect("~/Dashboard.aspx");
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            lblClientName.Text = String.Empty;
            string clientName = txtClientName.Text.Trim();


            txtClientName.CssClass = "input-error";
            // Kiem tra khac rong
            if (!clientName.Equals(""))
            {
                txtClientName.CssClass = "valid";
            }


            if (!clientName.Equals(""))
            {
                EnClient client = new EnClient();
                client.clientName = clientName;
                client.url = txtURL.Text.Trim();
                client.clientDes = txtDescription.Text.Trim();
                client.clientAddress = txtAddress.Text.Trim();
                client.nameAlt = txtNameAlt.Text.Trim();
                client.contactPerson = txtNameContact.Text.Trim();
                client.email = txtEmail.Text.Trim();
                client.phone = txtPhone.Text.Trim();
                client.fax = txtFax.Text.Trim();
                client.clientID = Int32.Parse(getParam());

                BuClient.updClient(client, Session["companyid"].ToString());
                Response.Redirect("~/time/admin/Client.aspx");

            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            BuClient.delClient(Int32.Parse(getParam()), Session["companyid"].ToString());
            Response.Redirect("~/time/admin/Client.aspx");
        }
    }
}